//
//  MeHelpViewController.m
//  CarUser
//
//  Created by 王森林 on 2019/1/16.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "MeHelpViewController.h"

@interface MeHelpViewController ()
@property (nonatomic, strong) NSMutableArray *listAry;
@property (nonatomic, strong) NSMutableArray *expandAry;
@end

#define IMAGETAG 100
@implementation MeHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帮助中心";
    [self fetchDataWithNet];
    // Do any additional setup after loading the view.
}

 - (void)fetchDataWithNet
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[CommonMethod createMD5Key:@"NULL"] forKey:@"FKEY"];
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_61 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        NSDictionary *result  = response;
        
        self.listAry = [NSMutableArray arrayWithArray:[result objectForKey:@"list"]];
        [self.mainTableView reloadData];
        
        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
}

- (void)handleTapAction:(UITapGestureRecognizer *)gesture
{
    UIView *headView = gesture.view;
    NSInteger tag = headView.tag;
    UIImageView *imageView = [headView viewWithTag:tag+ IMAGETAG];
    if (self.expandAry.count > 0 ) {
        if ([self.expandAry containsObject:[NSString stringWithFormat:@"%ld",tag]]) {
             //已经包含
            [self.expandAry removeObject:[NSString stringWithFormat:@"%ld",tag]];
            imageView.image = [UIImage imageNamed:@"help_down"];
        } else {
             [self.expandAry addObject:[NSString stringWithFormat:@"%ld",tag]];
              imageView.image = [UIImage imageNamed:@"help_up"];
            
        }
        
        
    } else {
        [self.expandAry addObject:[NSString stringWithFormat:@"%ld",tag]];
        imageView.image = [UIImage imageNamed:@"help_up"];
        
    }
    
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:tag];
    [self.mainTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSMutableArray *)expandAry
{
    if (!_expandAry) {
        _expandAry = [[NSMutableArray alloc] init];
    }
    return _expandAry;
}


#pragma mark ----UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1+self.listAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    if (self.expandAry.count  > 0) {
        if ([self.expandAry containsObject:[NSString  stringWithFormat:@"%ld",section]]) {
            return 1;
        }
    }
    return 0;
  
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *header = [[UIView alloc]init];
        header.backgroundColor = self.view.backgroundColor;
        
        UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 100)];
        headImageView.image = [UIImage imageNamed:@"me_help"];
        [header addSubview:headImageView];
        
        
        UILabel *headtTitleLabel  = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, BOUNDWIDTH  - 30, 25)];
        headtTitleLabel.text = @"帮助中心";
        headtTitleLabel.font = FONT_SIZE(24);
        headtTitleLabel.textColor = [UIColor whiteColor];
        [header addSubview:headtTitleLabel];
        
        UILabel *headtSubTitleLabel  = [[UILabel alloc] initWithFrame:CGRectMake(15, 60, BOUNDWIDTH  - 30, 18)];
        headtSubTitleLabel.text = @"我们用心为您答疑解惑";
        headtSubTitleLabel.font = FONT_SIZE(16);
        headtSubTitleLabel.textColor = RGB(211, 211, 211);
        [header addSubview:headtSubTitleLabel];
        
        
        
        UILabel *titleLabel  = [[UILabel alloc] initWithFrame:CGRectMake(15, 112, BOUNDWIDTH  - 30, 14)];
        titleLabel.text = @"常见问题";
        titleLabel.font = FONT_SIZE(16);
        titleLabel.textColor = RGB(153, 153, 153);
        [header addSubview:titleLabel];
        return header;
    }
    UIView *header = [[UIView alloc]init];
    header.tag = section;
    header.backgroundColor = [UIColor whiteColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
    [header addGestureRecognizer:tap];
    
    NSDictionary *result  = self.listAry[section-1];
    UILabel *titleLabel  = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, BOUNDWIDTH  - 50, 20)];
    titleLabel.text = [NSString stringWithFormat:@"%ld、%@",section,[result objectForKey:@"TITLE0"]];
    titleLabel.font = FONT_SIZE(14);
    titleLabel.textColor = Common_HeadTitle_Color;
    [header addSubview:titleLabel];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(BOUNDWIDTH - 30, 16, 14, 8)];
    headImageView.image = [UIImage imageNamed:@"help_down"];
    headImageView.tag = IMAGETAG  + section;
    [header addSubview:headImageView];
    
    
    
    
    if (self.expandAry.count > 0 ) {
        if ([self.expandAry containsObject:[NSString stringWithFormat:@"%ld",section]]) {
            //已经包含
            headImageView.image = [UIImage imageNamed:@"help_up"];
        } else {
          
            headImageView.image = [UIImage imageNamed:@"help_down"];
            
        }
        
        
    } 
    
    return header;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 138;
    }
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return  0;
    if ([self.expandAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
        //已经包含
        
        NSDictionary *result = self.listAry[indexPath.section  - 1];
         NSString *str = [result objectForKey:@"ANSWER"];
        
         CGSize size = [CommonMethod labWidthFromStr:str andFont:FONT_SIZE(14) andWidth:BOUNDWIDTH -30];
        
        if (size.height > 20) {
             return size.height+15;
        } else  {
            
            return 44;
        }
       
    } else {
        
        return 0;
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *brand_region_Cell = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:brand_region_Cell];

     if (cell == nil)
            {
              cell = [[UITableViewCell alloc] initWithStyle:
                                             UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%@%ld",@"MyCell",indexPath.section]];
            
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                　　　　　
                cell.backgroundColor = self.view.backgroundColor;
                NSDictionary *result = self.listAry[indexPath.section  - 1];
                NSString *str = [result objectForKey:@"ANSWER"];
                CGSize size = [CommonMethod labWidthFromStr:str andFont:FONT_SIZE(14) andWidth:BOUNDWIDTH -30];
              
                UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, BOUNDWIDTH- 30, size.height)];
                
                if (size.height  <  20) {
                    contentLabel.frame = CGRectMake(15, 15, BOUNDWIDTH- 30, 15);
                }
                contentLabel.text =  str;
                contentLabel.font = FONT_SIZE(14);
                contentLabel.textColor = RGB(68, 68, 68);
                contentLabel.numberOfLines = 0;
                [cell.contentView addSubview:contentLabel];
                
//                cell.textLabel.text = [result objectForKey:@"ANSWER"];
//                cell.textLabel.font = FONT_SIZE(14);
//                cell.textLabel.textColor = RGB(68, 68, 68);
//                cell.textLabel.numberOfLines = 0;
            　　　　 }
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
    　　　　return cell;

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
